// pages/order/waimaiorder.js
const app = getApp();
const nt_util = require('../../utils/nt_util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: {},
    buttondisabled: false,
    order_goods: [],
    queryBean:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    nt_util.that = this;
    var queryBean = JSON.parse(options.queryBean);
    nt_util.that.setData({
      order: app.globalData.order.cart,
      order_goods: app.globalData.order.cart.order_goods,
      queryBean: queryBean
    })

  },


  saveOrder: function () {

    if (nt_util.that.data.buttondisabled){
      return
    }
    nt_util.that.setData({
      buttondisabled: true
    })
    

    let _order = this.data.order;
    let buyerId = nt_util.getBuyerId();
    let repastId = nt_util.that.data.queryBean.repastId;
    let repastStartTime = nt_util.that.data.queryBean.repastStartTime;
    let repastEndTime = nt_util.that.data.queryBean.repastEndTime;

    var param = {
      "buyerId": buyerId,
      "orderType": repastId,
      "repastId": repastId,
      "repastStartTime": repastStartTime,
      "repastEndTime": repastEndTime,
      "total": _order.total*100,
      "productList": this.data.order_goods,
      "status":"1"
    };

    nt_util.httpRequestBody(nt_util.api.saveOrder, 'post', param, function (data) {
      if (data.code == 0 || data.code == 100){
        wx.hideLoading();
        wx.showToast({
          title: '提交成功',
          showCancel: false,

          success: function () {
            setTimeout(function () {
              wx.navigateBack({
                delta: 5,
              })
            }, 1500);
          }
        })
      } else {
        if (data.code == -1) {
          wx.showModal({
            content: data.msg,
            showCancel: false,
            success: function (e) { }
          });
        }
        nt_util.that.setData({
          buttondisabled: false
        })
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

})